package edu.bu.grocerylist.service;

import java.util.List;

import edu.bu.grocerylist.dao.IGroceryListDao;
import edu.bu.grocerylist.dao.IGroceryListItemDao;
import edu.bu.grocerylist.dao.IGroceryUserDao;
import edu.bu.grocerylist.model.GroceryList;
import edu.bu.grocerylist.model.GroceryUser;

/**
 * 
 * Service for managing GroceryUser objects
 * 
 * @author donlanp
 * 
 */
public class GroceryUserService implements IGroceryUserService {

	private IGroceryUserDao groceryUserDao;
	
	
	private IGroceryListDao groceryListDao;
	private IGroceryListItemDao groceryListItemDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.bu.grocerylist.service.IGroceryUserService#getAllUsers()
	 */
	public List<GroceryUser> getAllUsers() {
		return groceryUserDao.getAllUsers();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.bu.grocerylist.service.IGroceryUserService#login(String, String)
	 */
	public GroceryUser login(String username, String password) {

		GroceryUser login = groceryUserDao.getUserByCredentials(username,password);
		
		return login;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.bu.grocerylist.service.IGroceryUserService#registerUser(GroceryUser)
	 */
	public GroceryUser registerUser(GroceryUser userToRegister) {
		// check if username already exists
		if(groceryUserDao.getUsersByUsername(userToRegister.getEmail()).size() == 0){
			groceryUserDao.saveUser(userToRegister);			
		} else {
			return null;
		}

		return userToRegister;
	}

	public GroceryUser updatePassword(GroceryUser user, String newPassword) {
		user.setPassword(newPassword);
		groceryUserDao.updateUser(user);
		return user;
	}

	public GroceryUser getUserSecurityQuestion(String userId) {
		GroceryUser user = groceryUserDao.getUserByUserId(userId);
		if (user != null) {

			user.setPassword(null);
		}
		return user;
	}
	

	public void deleteUserFromSystem(GroceryUser userToDelete) {
		//First, delete all of the user's lists and items
		List<GroceryList> lists = groceryListDao.getGroceryListsByUser(userToDelete.getUserId());
		
		for(GroceryList groceryList : lists){
			groceryListItemDao.deleteAllGroceryListItemsFromList(groceryList);			
		}
		
		groceryListDao.deleteGroceryListsOfUser(userToDelete);
		
		//Finally delete the user
		groceryUserDao.deleteUser(userToDelete);
		
	}
	
	
	//Getters and Setters
	


	/**
	 * @return the groceryUserDao
	 */
	public IGroceryUserDao getGroceryUserDao() {
		return groceryUserDao;
	}

	/**
	 * @param groceryUserDao
	 *            the groceryUserDao to set
	 */
	public void setGroceryUserDao(IGroceryUserDao groceryUserDao) {
		this.groceryUserDao = groceryUserDao;
	}
	public IGroceryListDao getGroceryListDao() {
		return groceryListDao;
	}
	public void setGroceryListDao(IGroceryListDao groceryListDao) {
		this.groceryListDao = groceryListDao;
	}
	public IGroceryListItemDao getGroceryListItemDao() {
		return groceryListItemDao;
	}
	public void setGroceryListItemDao(IGroceryListItemDao groceryListItemDao) {
		this.groceryListItemDao = groceryListItemDao;
	}

}
